/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResourceListPanel.java
 *
 * Created on Sep 16, 2011, 7:24:30 PM
 */

package com.agentslab.gameprovider;

import aa.core.ActorMessage;
import aa.core.ActorName;
import aa.core.Contract;
import aa.core.Platform;
import aa.tool.CyberOrgTuple;
import com.agentslab.appstarter.MainFrame;
import com.agentslab.constants.APIConstants;
import javax.swing.JFrame;
import javax.swing.JList;

/**
 *
 * @author mehadihasan
 */
public class ResourceListPanel extends javax.swing.JPanel {

    /** Creates new form ResourceListPanel */
    public ResourceListPanel() {
        
        this.platform = APIConstants.platform;
        initComponents();
    }

       public Contract getProposedContract() {

        try{
            this.proposedContract = new Contract(Integer.parseInt(this.nTicksTextField.getText()), Integer.parseInt(this.tickRateTextField.getText()), Integer.parseInt(this.priceTextField.getText()));
        } catch(Exception e){
            this.setProposedContract();
        }
        return proposedContract;
    }

    public void setProposedContract() {
        this.proposedContract = new Contract(1000000,1000,1);
    }

    public JList getResourceList() {
        return resourceList;
    }

    public void setResourceList(JList resourceList) {
        this.resourceList = resourceList;
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        tickRateTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        nTicksTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        resourceList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        negotiateButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        gameNameTextField = new javax.swing.JTextField();

        jLabel1.setText("Please enter the following information to search and negotiate for a resource");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        tickRateTextField.setText("1000");

        jLabel6.setText("Ticks rate: ");

        priceTextField.setText("1");

        jLabel7.setText("Proposed price:");

        nTicksTextField.setText("1000000");

        jLabel5.setText("# of ticks required: ");

        jScrollPane1.setViewportView(resourceList);

        jLabel2.setText("Available resources:");

        negotiateButton.setText("Negotiate and start server");
        negotiateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                negotiateButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Game name");

        gameNameTextField.setText("pacman");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addContainerGap(133, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                        .add(180, 180, 180))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel7)
                                    .add(jLabel6)
                                    .add(jLabel5))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 53, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(priceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                    .add(tickRateTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                    .add(nTicksTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                    .add(gameNameTextField))
                                .add(105, 105, 105))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 161, Short.MAX_VALUE)
                                .add(searchButton)))
                        .add(180, 180, 180))
                    .add(layout.createSequentialGroup()
                        .add(jLabel3)
                        .addContainerGap(447, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(negotiateButton)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))
                        .add(180, 180, 180))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(gameNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nTicksTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tickRateTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(priceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel7))
                .add(8, 8, 8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(searchButton)
                    .add(jLabel2))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(negotiateButton)
                .add(32, 32, 32))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void negotiateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_negotiateButtonActionPerformed
        // TODO add your handling code here:
        CyberOrgTuple cyberOrgTuple = (CyberOrgTuple)this.getResourceList().getSelectedValue();
        cyberOrgTuple.setGameName(this.gameNameTextField.getText());
        Object[] argsreg=new Object[4];
        argsreg[0] = cyberOrgTuple;
        argsreg[1]= this.getProposedContract();
        argsreg[2] = new ActorName(cyberOrgTuple.getDestination(),8);
        argsreg[3] = ""+System.currentTimeMillis();
        ActorMessage am_reg = new ActorMessage(platform.getActorNameOfPlatform(),
                                               APIConstants.resourceBroker,
                                               "negotiate",
                                               argsreg,
                                               false
                                               );

        platform.sendMessage(am_reg);
    }//GEN-LAST:event_negotiateButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
        Object[] argsreg=new Object[4];
        argsreg[0]= this.getProposedContract();
        argsreg[1]= "pacman";
        argsreg[2] = APIConstants.guiActor;
        argsreg[3] = ""+System.currentTimeMillis();
        ActorMessage am_reg = new ActorMessage(platform.getActorNameOfPlatform(),
                                               APIConstants.resourceBroker,
                                               "getNewResource",
                                               argsreg,
                                               false
                                               );

        platform.sendMessage(am_reg);
    }//GEN-LAST:event_searchButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField gameNameTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField nTicksTextField;
    private javax.swing.JButton negotiateButton;
    private javax.swing.JTextField priceTextField;
    private javax.swing.JList resourceList;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField tickRateTextField;
    // End of variables declaration//GEN-END:variables
    private final Platform platform;
    private Contract proposedContract;


}
